package com.example.its2021606134chh.DAModule;

import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.its2021606134chh.R;
import com.example.its2021606134chh.TrafficViolationModule.CarSQLiteOpenHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.ColumnChartView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TimeTVFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TimeTVFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ColumnChartView timeChart;
    private ColumnChartData mColumnChartData;    //柱状图数据
    public final static String[] xDate = {"0:00:01-2:00:00", "2:00:01-4:00:00", "4:00:01-6:00:00",
            "6:00:01-8:00:00", "8:00:01-10:00:00", "10:00:01-12:00:00", "12:00:01-14:00:00",
            "14:00:01-16:00:00", "16:00:01-18:00:00", "18:00:01-20:00:00", "20:00:01-22:00:00",
            "22:00:01-24:00:00"}; //x

    public TimeTVFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TimeTVFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static TimeTVFragment newInstance(String param1, String param2) {
        TimeTVFragment fragment = new TimeTVFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_time_t_v, container, false);
        timeChart = (ColumnChartView) view.findViewById(R.id.timeChart);
        CarSQLiteOpenHelper openHelper;
        SQLiteDatabase database;
        openHelper = new CarSQLiteOpenHelper(getContext(), "CarPeccancy.db", null, 1);
        database = openHelper.getReadableDatabase();
        Set<String> stringSet = new HashSet<String>();
        Map<String, Integer> stringMap = new HashMap<String, Integer>();
        List<String> times = new ArrayList<String>();
        String sTime;
        try (Cursor cursor = database.query("carpeccancy", null, null, null, null, null, null)) {
            if (cursor.moveToFirst()) {
                do {
                    String pdatetime = cursor.getString(2);
//                    偷懒
                    String time = pdatetime.substring(11, 13);
//                    Log.i("TAG", time);
                    times.add(time);
                } while (cursor.moveToNext());
            }
        } catch (SQLException ignored) {
            Toast.makeText(getContext(), "没有查询到违章数据！", Toast.LENGTH_SHORT).show();
        } finally {
            database.close();
        }

        for (int i = 0; i < times.size(); i++) {
            sTime = times.get(i);
            if (stringSet.contains(sTime)) {
                stringMap.put(sTime, stringMap.get(sTime) + 1);
            } else {
                stringSet.add(sTime);
                stringMap.put(sTime, 1);
            }
        }

        int allC;
        int c0 = 0;
        int c1 = 0;
        int c2 = 0;
        int c3 = 0;
        int c4 = 0;
        int c5 = 0;
        int c6 = 0;
        int c7 = 0;
        int c8 = 0;
        int c9 = 0;
        int c10 = 0;
        int c11 = 0;

        for (String s : stringSet) {
//            Log.i("TAG", s + " " + stringMap.get(s));
            if (s.equals("00")) {
                c0++;
            } else if (s.equals("01")) {
                c0++;
            } else if (s.equals("02")) {
                c1++;
            } else if (s.equals("03")) {
                c1++;
            } else if (s.equals("04")) {
                c2++;
            } else if (s.equals("05")) {
                c2++;
            } else if (s.equals("06")) {
                c3++;
            } else if (s.equals("07")) {
                c3++;
            } else if (s.equals("08")) {
                c4++;
            } else if (s.equals("09")) {
                c4++;
            } else if (s.equals("10")) {
                c5++;
            } else if (s.equals("11")) {
                c5++;
            } else if (s.equals("12")) {
                c6++;
            } else if (s.equals("13")) {
                c6++;
            } else if (s.equals("14")) {
                c7++;
            } else if (s.equals("15")) {
                c7++;
            } else if (s.equals("16")) {
                c8++;
            } else if (s.equals("17")) {
                c8++;
            } else if (s.equals("18")) {
                c9++;
            } else if (s.equals("19")) {
                c9++;
            } else if (s.equals("20")) {
                c10++;
            } else if (s.equals("21")) {
                c10++;
            } else if (s.equals("22")) {
                c11++;
            } else if (s.equals("23")) {
                c11++;
            }
        }
        allC = stringSet.size();

        BigDecimal b0  = BigDecimal.valueOf((float) c0 / (float) allC * 100);
        BigDecimal b1  = BigDecimal.valueOf((float) c1 / (float) allC * 100);
        BigDecimal b2  = BigDecimal.valueOf((float) c2 / (float) allC * 100);
        BigDecimal b3  = BigDecimal.valueOf((float) c3 / (float) allC * 100);
        BigDecimal b4  = BigDecimal.valueOf((float) c4 / (float) allC * 100);
        BigDecimal b5  = BigDecimal.valueOf((float) c5 / (float) allC * 100);
        BigDecimal b6  = BigDecimal.valueOf((float) c6 / (float) allC * 100);
        BigDecimal b7  = BigDecimal.valueOf((float) c7 / (float) allC * 100);
        BigDecimal b8  = BigDecimal.valueOf((float) c8 / (float) allC * 100);
        BigDecimal b9  = BigDecimal.valueOf((float) c9 / (float) allC * 100);
        BigDecimal b10  = BigDecimal.valueOf((float) c10 / (float) allC * 100);
        BigDecimal b11  = BigDecimal.valueOf((float) c11 / (float) allC * 100);
        float f0 = b0.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        float f1 = b1.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        float f2 = b2.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        float f3 = b3.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        float f4 = b4.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        float f5 = b5.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        float f6 = b6.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        float f7 = b7.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        float f8 = b8.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        float f9 = b9.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        float f10 = b10.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        float f11 = b11.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        float[] score= {f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11};
        //颜色值
        List<Integer> color = new ArrayList<>();
        color.add(Color.parseColor("#009BDB"));
        color.add(Color.parseColor("#85B74F"));
        color.add(Color.parseColor("#ff0000"));
        color.add(Color.parseColor("#009BDB"));
        color.add(Color.parseColor("#85B74F"));
        color.add(Color.parseColor("#ff0000"));
        color.add(Color.parseColor("#009BDB"));
        color.add(Color.parseColor("#85B74F"));
        color.add(Color.parseColor("#ff0000"));
        color.add(Color.parseColor("#009BDB"));
        color.add(Color.parseColor("#85B74F"));
        color.add(Color.parseColor("#ff0000"));

        List<Column> columnList = new ArrayList<>(); //柱子列表
        List<SubcolumnValue> subcolumnValueList;     //子柱列表（即一个柱子，因为一个柱子可分为多个子柱）
        List<AxisValue> axisValues = new ArrayList<>();//自定义横轴坐标值
        for (int i = 0; i < 12; ++i) {
            subcolumnValueList = new ArrayList<>();
            subcolumnValueList.add(new SubcolumnValue(score[i], color.get(i)));

            Column column = new Column(subcolumnValueList);
            columnList.add(column);
            column.setHasLabels(true);
            axisValues.add(new AxisValue(i).setLabel(xDate[i]));
        }

        mColumnChartData = new ColumnChartData(columnList);
        Axis axisX = new Axis(axisValues);
        axisX.setHasTiltedLabels(true);
        //字体颜色
        axisX.setTextColor(Color.parseColor("#666666"));
        //字体大小
        axisX.setTextSize(10);
        Axis axisY = new Axis().setHasLines(true);
        mColumnChartData.setAxisXBottom(axisX);
        mColumnChartData.setAxisYLeft(axisY);

        timeChart.setColumnChartData(mColumnChartData);
        Viewport v = timeChart.getMaximumViewport();
        v.top = 28;
        timeChart.setCurrentViewport(v);
        return view;
    }

}